<extend name="Layout/ins_page" />
<block name="content">
	{:W('PageHeader/simple',array('name'=>'修改任务','search'=>'N'))}

	<form method='post' id="form_data" name="form_data" enctype="multipart/form-data"   class="well form-horizontal">
		<input type="hidden" id="id" name="id" value="{$vo.id}">
		<input type="hidden" id="ajax" name="ajax" value="0">
		<input type="hidden" id="executor" name="executor" value="{$vo.executor}">
		<input type="hidden" id="assign_user" name="assign_user" value="{$vo.assign_user}">
		<input type="hidden" id="in_check" name="in_check" value="{$vo.check_user_name}|{$vo.check_user_id};" />
		<input type="hidden" id="opmode" name="opmode" value="edit">

		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">标题：</label>
			<div class="col-sm-10">
				<input class="form-control" type="text" id="name" name="name" value="{$vo.name}"  check="require" msg="请输入文件名">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">指派给：</label>
			<div class="col-sm-10">
				<div id="actor_wrap" class="inputbox">
					<a class="pull-right btn btn-link text-center" onclick="popup_actor();"><i class="fa fa-user"></i> </a>
					<div class="wrap" >
						<span class="address_list">{$vo.executor|show_contact="edit"}</span>
						<span class="text" >
							<input class="letter" type="text"  >
						</span>
					</div>
					<div class="search dropdown ">
						<ul class="dropdown-menu"></ul>
					</div>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="plan_time">期望完成时间：</label>
			<div class="col-sm-10">
				<input class="form-control input-date-time" type="text" id="expected_time" name="expected_time" value="{$vo.expected_time}"readonly="readonly" >
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="content">任务说明：</label>
			<div class="col-xs-10">
				<textarea class="editor" id="content" name="content" style="width:100%;height:300px;" check="require" msg="请输入内容">{$vo.content}</textarea>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label" for="name">附件：</label>
			<div class="col-sm-10">
				{:W('FileUpload/edit',array('add_file'=>$vo['add_file'],'mode'=>'add'))}
			</div>
		</div>
		<div class="form-group is-in-check">
			<label class="col-sm-2 control-label" for="name">是否入库：</label>
			<div class="col-sm-8">
				<input class="ace"  type="radio" name="is_check_in" <if condition="$vo['is_check_in'] eq 0">checked="checked"</if> value="0"/>否 &nbsp; &nbsp;
				<input class="ace"  type="radio" name="is_check_in" <if condition="$vo['is_check_in'] eq 1">checked="checked"</if> value="1"/>是
			</div>
		</div>
		
		<div class="form-group is-audit">
			<label class="col-sm-2 control-label" for="name">是否审核：</label>
			<div class="col-sm-8">
				<input class="ace"  type="radio" name="is_audit" <if condition="$vo['is_audit'] eq 0">checked="checked"</if> value="0"/>否 &nbsp; &nbsp;
				<input class="ace"  type="radio" name="is_audit" <if condition="$vo['is_audit'] eq 1">checked="checked"</if> value="1"/>是
			</div>
		</div>
		
		<div class="form-group in-parent-check <if condition="$vo['is_audit'] eq 0">hide</if>">
			<label class="col-sm-2 control-label" for="name">入库审核：</label>
			<div class="col-sm-10">
				<div id="in_wrap" class="inputbox">
					<a class="pull-right btn btn-link text-center" onclick="popup_in();"><i class="fa fa-user"></i> </a>
					<div class="wrap" >
					    <php>$gg = $vo['check_user_name'].'|'.$vo['check_user_id'].';'</php>
						<span class="address_list">
							<if condition=" $vo['check_user_id'] neq 0">  {$gg|show_contact="edit"} </if>
						</span>
						<span class="text" >
							<input class="letter" type="text" />
						</span>
					</div>
					<div class="search dropdown">
						<ul class="dropdown-menu"></ul>
					</div>
				</div>
			</div>
		</div>
		<notempty name="task_product">
				<volist name="task_product" id="item" key="k" >
					<div class="form-group parent-add-task-product  <if condition = "$vo['is_audit'] eq 1">hide</if>">
						<label class="col-sm-2 control-label" for="name">
						 	<if condition="($k eq 1)">发布生产产品：</if>
						</label>
						<div class="add-task-product">
							<div class="col-sm-2">
								<input class="form-control" type="text"  name="product[]" placeholder="产品" value="{$item.product}"  msg="请输入产品" />
								<input class="form-control" type="text"  name="number[]" placeholder="数量" value="{$item.number}"  msg="请输入数量" />
								<input class="form-control" type="text"  name="product_price[]" placeholder="产品单价"  value="{$item.product_price}"  msg="请输入产品单价" />
							</div>
							<if condition="($k eq 1)">
								<a  class="btn btn-sm btn-primary product-add" >添加</a>
							<else />
								<a  class="btn btn-sm btn-primary product-del" >删除</a>
							</if>
						</div>
					</div>
				</volist>
		    	<volist name="task_product" id="sitem" key="sk" >
					<div class="form-group parent-add-task-product-check-in <if condition="$vo['is_audit'] eq 0">hide</if>">
						<label class="col-sm-2 control-label" for="name">
						 	<if condition="($sk eq 1)">发布生产产品：</if>
						</label>
						<div class="add-task-product">
							<div class="col-sm-2">
								<select id="storehouse_product_id" name="storehouse_product_id[]"  class="form-control product-check-in"  msg="请输入入库产品">
									<option value="">请选择</option>
									<foreach name="product" item="voe">
										<option value="{$voe.id}" <if condition="($voe['id'] eq $sitem['storehouse_product_id'])">selected="selected"</if>>{$voe.product}--{$voe.product_bh}</option>
									</foreach>
								</select>
								<input class="form-control" type="text"  name="in_number[]" placeholder="数量" value="{$sitem.number}"  msg="请输入数量" />
								<input class="form-control" type="text"  name="in_product_price[]" placeholder="产品单价"  value="{$sitem.product_price}"  msg="请输入产品单价" />
							</div>
							<if condition="($sk eq 1)">
								<a  class="btn btn-sm btn-primary product-add" >添加</a>
							<else />
								<a  class="btn btn-sm btn-primary product-del" >删除</a>
							</if>
						</div>
					</div>
				</volist>
		</notempty>
		
		<div class="form-group">
			<div class="action col-sm-10 col-sm-offset-2">
				<input class="btn btn-sm btn-primary" type="button" value="保存" onclick="save();">
				<a class="btn btn-sm btn-default" type="button" href="javascript:;" onClick="javascript:history.go(-1);" />取消</a>
			</div>
		</div>
	</form>
</block>
<block name="js">
	<script type="text/javascript">
		function save() {
			
			assign();
			check();
			sendForm("form_data", "{:U('save')}");
		}
		
		function assign() {
			
			$("#executor").val("");
			if ($("#actor_wrap span.address_list span").size()<=0) {
				ui_error('请选择指派人员');
				return false;
			}
			$("#actor_wrap span.address_list span").each(function() {
				$("#executor").val($("#executor").val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");
			});
			$html = $("#executor").val();
			var re = new RegExp("dept_", "g");
			var arr = $html.match(re);
			if (arr != null) {
				$dept_count = arr.length;
				$total_count = $html.split(';').length - 1;
				if ($dept_count != $total_count) {
					ui_error('部门和人员不能同时选择');
					return false;
				}
			}
		}
		
		function check() {
			
			var is_audit = $('.is-audit').find('input:checked').val();
			$("#in_check").val("");
			if (is_audit==1) {
				if ($("#in_wrap span.address_list span").size()>1) {
					ui_error('审核人员有且只有一个');
					return false;
				}
				$("#in_wrap span.address_list span").each(function() {
					$("#in_check").val($("#in_check").val() + $(this).find("b").text() + '|' + $(this).attr("data") + ";");
				});
				$html = $("#in_check").val();
				var re = new RegExp("dept_", "g");
				var arr = $html.match(re);
				if (arr != null) {
					ui_error('部门不能选择');
					return false;
				}
			}
		}
		
		

		function popup_actor() {
			winopen("{:U('popup/task')}",560, 470);
		}
		
		function popup_in() {
			winopen("{:U('popup/in')}",560, 470);
		}

		$(document).ready(function() {
			$(document).on("click", ".inputbox .address_list a.del", function() {
				$(this).parent().parent().remove();
			});
			
			$('.parent-add-task-product').on('click','.product-add',function(e){ //添加
				
				var html = '<div class="form-group ff">'
						      +'<label class="col-sm-2 control-label" for="name"></label>'
						      +'<div class="add-task-product">'
						      +'<div class="col-sm-2">'
						      +'<input class="form-control" type="text"  name="product[]"  placeholder="产品">'
						      +'<input class="form-control" type="text"  name="number[]" placeholder="数量" />'
						      +'<input class="form-control" type="text"  name="product_price[]" placeholder="产品单价"   check="require" msg="请输入产品单价" />'
						      +'</div>'
						      +'<span class="btn btn-sm btn-primary product-del">删除</span>'
						      +'</div>'
						      +'</div>';
				$('.parent-add-task-product:first').after(html);
				e.preventDefault();
				
			});
			
			$('.parent-add-task-product-check-in').on('click','.product-add',function(e){  // 添加
				
				var select = $('.product-check-in').html();
				var html = '<div class="form-group gg">'
				      +'<label class="col-sm-2 control-label" for="name"></label>'
				      +'<div class="add-task-product">'
				      +'<div class="col-sm-2">'
				      +'<select id="storehouse_product_id" name="storehouse_product_id[]" class="form-control product-check-in" >'
				      +select
				      +'</select>'
				      +'<input class="form-control" type="text"  name="in_number[]" placeholder="数量" />'
				      +'<input class="form-control" type="text"  name="in_product_price[]" placeholder="产品单价" />'
				      +'</div>'
				      +'<span class="btn btn-sm btn-primary product-del">删除</span>'
				      +'</div>'
				      +'</div>';
				$('.parent-add-task-product-check-in:first').after(html);
				e.preventDefault();
			})
			
			$('.is-in-check').on('click','.ace',function(event) {
				if ($(this).val()==0) {
					$('.in-parent-check').addClass('hide');
					$('.parent-add-task-product').removeClass('hide');
					$('.parent-add-task-product-check-in').addClass('hide');
					$('.gg').remove();
				} else {
					$('.in-parent-check').removeClass('hide');
					$('.parent-add-task-product').addClass('hide');
					$('.parent-add-task-product-check-in').removeClass('hide');
					$('.ff').remove();
				}
			})
			
			$('#form_data').on('click','.product-del',function(event){ //删除
				$(this).parents('.form-group').remove();
			})
		});

	</script>
</block>